package com.phuc.app.vuivui;

import com.phuc.app.vuivui.sqlite.DatabaseAdapter;
import com.phuc.app.vuivui.utils.Utils;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import static com.phuc.app.vuivui.utils.Constants.*;

/**
 * Class Main menu activity.
 * @author MonteCristo
 *
 */
public class MainMenuActivity extends Activity {

	/**
	 * Popup confirm exit application.
	 */
	private AlertDialog.Builder confirmExit;
	
	private AlertDialog.Builder confirmInternet;

	public static final String SHARE_PREFER_THEME_KEY = "SHARE_PREFER_THEME_KEY";
	int selecedTheme;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Get selected theme
		SharedPreferences prefs = getPreferences(MODE_PRIVATE);
		selecedTheme = prefs.getInt(SHARE_PREFER_THEME_KEY, 1);
		if (selecedTheme == 2) {
			setContentView(R.layout.main_menu);
		} else if (selecedTheme == 3) {
			setContentView(R.layout.main_menu_3);
		} else {
			setContentView(R.layout.main_menu_2);
		}

		// Initialize alert dialog
		initAlert();
		DatabaseAdapter.initHelper(this);
		
		Toast.makeText(this, R.string.select_theme, Toast.LENGTH_LONG).show();
	}

	@Override
	protected void onStart() {
		super.onStart();
		
		if (!isNetworkConnected()) {
			confirmInternet.show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_menu, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
		
		// Handle event when user select theme
		switch (item.getItemId()) {
		case R.id.menu_choose_1:
			selecedTheme = 1;
			setContentView(R.layout.main_menu_2);
			break;
		case R.id.menu_choose_2:
			selecedTheme = 2;
			setContentView(R.layout.main_menu);
			break;
		case R.id.menu_choose_3:
			selecedTheme = 3;
			setContentView(R.layout.main_menu_3);
			break;
		default:
			break;
		}
		
		editor.putInt(SHARE_PREFER_THEME_KEY, selecedTheme);
		editor.commit();
		return false;
	}
	
	private void initAlert() {
		// Confirm exit
    	this.confirmExit = new AlertDialog.Builder(this);
    	this.confirmExit.setMessage(R.string.confirm_quit);
    	this.confirmExit.setCancelable(false);
    	this.confirmExit.setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int id) {
				MainMenuActivity.this.finish();
			}
		});
    	this.confirmExit.setNegativeButton(R.string.btn_no, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
    	
    	this.confirmInternet = new AlertDialog.Builder(this);
    	this.confirmInternet.setMessage(R.string.need_internet_connection);
    	this.confirmInternet.setCancelable(false);
    	this.confirmInternet.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int id) {
				MainMenuActivity.this.finish();
			}
		});
	}
	
	/**
	 * Method handle event when button is clicked.
	 * @param v the clicked button
	 */
	public void buttonClickAction(View v) {
		Intent intent;
		
		ReadingActivity.isLoadFirstTime = true;
		
		switch (v.getId()) {
		case R.id.mainMenu_btnReadAll:
			intent = new Intent(this, ReadingActivity.class);
			intent.setAction(INTENT_ACTION_START_READ);
			intent.putExtra(INTENT_PARAM_READ_TYPE, ReadingActivity.READ_TYPE_ALL);
			intent.putExtra(SHARE_PREFER_THEME_KEY, selecedTheme);
			startActivity(intent);
			break;
		case R.id.mainMenu_btnReadText:
			intent = new Intent(this, ReadingActivity.class);
			intent.setAction(INTENT_ACTION_START_READ);
			intent.putExtra(INTENT_PARAM_READ_TYPE, ReadingActivity.READ_TYPE_TEXT);
			intent.putExtra(SHARE_PREFER_THEME_KEY, selecedTheme);
			startActivity(intent);
			break;
		case R.id.mainMenu_btnReadImage:
			intent = new Intent(this, ReadingActivity.class);
			intent.setAction(INTENT_ACTION_START_READ);
			intent.putExtra(INTENT_PARAM_READ_TYPE, ReadingActivity.READ_TYPE_IMAGE);
			intent.putExtra(SHARE_PREFER_THEME_KEY, selecedTheme);
			startActivity(intent);
			break;
		case R.id.mainMenu_btnReadLike:
			intent = new Intent(this, ReadingActivity.class);
			intent.setAction(INTENT_ACTION_START_READ);
			intent.putExtra(INTENT_PARAM_READ_TYPE, ReadingActivity.READ_TYPE_LIKE);
			intent.putExtra(SHARE_PREFER_THEME_KEY, selecedTheme);
			startActivity(intent);
			break;
		case R.id.mainMenu_btnBack:
			this.confirmExit.show();
			break;
		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		this.confirmExit.show();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Utils.unbindDrawable(findViewById(R.id.root));
        System.gc();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (ReadingActivity.listId != null) {
			ReadingActivity.listId.clear();
		}
		System.gc();
	}
	

	private boolean isNetworkConnected() {
	  ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	  NetworkInfo ni = cm.getActiveNetworkInfo();
	  if (ni == null) {
		  return false;
	  } else{
		  return true;
	  }
	}
}
